import Vue from 'vue'
import App from './App.vue'
import Vuex from 'vuex'

Vue.use(Vuex)
const store = new Vuex.Store({
  //数据管理
  state: {
    goodList: [
      {
        name: '诸葛亮',
        price: 1000,
        num: 1,
        checked: false,
      },
      {
        name: '蔡文姬',
        price: 1500,
        num: 1,
        checked: false,
      },
      {
        name: '妲己',
        price: 2000,
        num: 1,
        checked: false,
      },
      {
        name: '鲁班',
        price: 2200,
        num: 1,
        checked: false,
      },
    ],
  },
  mutations: {
    sum(state, ind) {
      console.log(ind)
      // console.log(1)
      state.goodList[ind].num += 1
    },
    minus(state, ind) {
      state.goodList[ind].num -= 1
    },
    remove(state, ind) {
      state.goodList = state.goodList.filter((item, index) => index !== ind)
    },
    changeAll(state, val) {
      state.goodList.forEach((item) => {
        item.checked = val
      })
    },
  },
  getters: {
    total(state) {
      const list = state.goodList.filter((item) => item.checked === true)
      return list.reduce((sum, item) => sum + item.price * item.num, 0)
    },
  },
})
Vue.config.productionTip = false

new Vue({
  store,
  render: (h) => h(App),
}).$mount('#app')
